package pers.cyril.jdb.client.command.handler;

import com.beust.jcommander.ParameterException;
import io.netty.channel.Channel;
import pers.cyril.jdb.client.command.CreateCommand;
import pers.cyril.jdb.protocol.packet.CreateDatabaseRequestPacket;
import pers.cyril.jdb.protocol.packet.CreateUserRequestPacket;

/**
 * 说点什么吧
 *
 * @author Wang Cyril
 * @date 2024/5/29 21:52
 */
public class CreateCommandHandler implements CommandHandler<CreateCommand> {
    @Override
    public void handle(CreateCommand command, Channel channel) {
        if(command.getDatabase() != null){
            //create database
            CreateDatabaseRequestPacket packet = new CreateDatabaseRequestPacket();
            packet.setDatabase(command.getDatabase());
            channel.writeAndFlush(packet);
        }else if(command.getUsername() != null){
            //create user
            CreateUserRequestPacket packet = new CreateUserRequestPacket();
            packet.setUsername(command.getUsername());
            packet.setPassword(command.getPassword());
            channel.writeAndFlush(packet);

        }else {
            throw new ParameterException("pls use create database or create user");
        }
    }
}
